package com.caac.core.web.servlet;

import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

import javax.servlet.ServletException;

import com.caac.core.config.ActionConf;
import com.caac.core.config.ActionSys;
import org.apache.struts.action.ActionServlet;
import org.apache.struts.config.ActionConfig;
import org.apache.struts.config.ModuleConfig;

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright Intolong 2004</p>
 * <p>Company: Intolong</p>
 * @author <a href="mailto:intolong@163.com">Intolong</a>
 * @version 1.0
 */

public class AppServlet extends ActionServlet {
    private static final String SUFFIX = ".do";

    protected ModuleConfig initModuleConfig(String prefix, String paths) throws
            ServletException {
        ModuleConfig moduleConfig = super.initModuleConfig(prefix, paths);
        List actionConfigs = sort(moduleConfig.findActionConfigs());
        for (int i = 0; i < actionConfigs.size(); i++) {
            ActionSys.add(parseParameter((ActionConfig) actionConfigs.get(i)));
        }
        return moduleConfig;
    }

    private static List sort(ActionConfig[] ac) {
        List other = new ArrayList();
        for (int i = 0; i < ac.length; i++) {
            other.add(ac[i]);
        }
        return other;
    }

    private static ActionConf parseParameter(ActionConfig actionConfig) {
        ActionConf result;
        StringTokenizer st = new StringTokenizer(actionConfig.getParameter(),
                                                 ",");
        String domain = null;
        String title = null;
        boolean authorized = false;
        boolean administrable = false;
        int index = 0;
        while (st.hasMoreTokens()) {
            index++;
            String token = st.nextToken().trim();
            if (token.length() == 0) {
                token = null;
            }
            if (index == 1) {
                if (token == null) {
                    throw new IllegalStateException(
                            "Domain of action should not be null.");
                }
                domain = token;
            } else if (index == 2) {
                if (token == null) {
                    throw new IllegalStateException(
                            "Title of action should not be null.");
                }
                title = token;
            } else if (index == 3) {
                if (token == null) {
                    throw new IllegalStateException(
                            "Authorized of action should not be null.");
                }
                authorized = Boolean.valueOf(token).booleanValue();
            } else if (index == 4) {
                if (token != null) {
                    administrable = Boolean.valueOf(token).booleanValue();
                }
            }
        }
        //System.out.println( title +","+actionConfig.getPath() + SUFFIX);
        result = new ActionConf(actionConfig.getPath() + SUFFIX, domain, title,
                                authorized, administrable);
        return result;
    }
}
